package com.hu.video.mapper;

import com.hu.video.entity.UserBlockRelations;
import com.hu.video.entity.dto.UserBlockDTO;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserBlockRelationsMapper {
    @Select("SELECT * FROM user_block_relations WHERE user_id = #{userId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "blockedUserId", column = "blocked_user_id"),
        @Result(property = "blockType", column = "block_type"),
        @Result(property = "createTime", column = "create_time")
    })
    List<UserBlockRelations> selectByUserId(Long userId);

    @Select("SELECT * FROM user_block_relations WHERE blocked_user_id = #{blockedUserId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "blockedUserId", column = "blocked_user_id"),
        @Result(property = "blockType", column = "block_type"),
        @Result(property = "createTime", column = "create_time")
    })
    List<UserBlockRelations> selectByBlockedUserId(Long blockedUserId);

    @Select("SELECT b.id, b.user_id, b.blocked_user_id, b.block_type, b.create_time, u.user_name as blocked_user_name " +
            "FROM user_block_relations b " +
            "LEFT JOIN t_user u ON b.blocked_user_id = u.user_id " +
            "WHERE b.user_id = #{userId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "blockedUserId", column = "blocked_user_id"),
        @Result(property = "blockedUserName", column = "blocked_user_name"),
        @Result(property = "blockType", column = "block_type"),
        @Result(property = "createTime", column = "create_time")
    })
    List<UserBlockDTO> selectUserBlocksWithNames(Long userId);

    @Select("SELECT COUNT(*) FROM user_block_relations WHERE user_id = #{userId} AND blocked_user_id = #{blockedUserId} AND block_type = #{blockType}")
    int checkExists(@Param("userId") Long userId, @Param("blockedUserId") Long blockedUserId, @Param("blockType") Integer blockType);

    @Insert("INSERT INTO user_block_relations (user_id, blocked_user_id, block_type, create_time) VALUES (#{userId}, #{blockedUserId}, #{blockType}, NOW())")
    int insert(UserBlockRelations block);

    @Delete("DELETE FROM user_block_relations WHERE user_id = #{userId} AND blocked_user_id = #{blockedUserId} AND block_type = #{blockType}")
    int delete(UserBlockRelations block);
} 